package com.qq.controller;

import cn.hutool.crypto.SecureUtil;
import com.qq.Service.UserService;
import com.qq.common.MyResponse;
import com.qq.data.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;

@Api(tags={"用户信息"},description = "用户信息增删改查")
@RestController
public class UserController {

    @Resource
    private UserService userervice;

    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/user/insert")
    public MyResponse rege(@RequestBody User user) {
        String password = user.getPassword();
        user.setPassword(SecureUtil.md5(password));
        LocalDateTime now = LocalDateTime.now();
        user.setCreateTime(now);
        userervice.saveUser(user);
        return MyResponse.success();
    }

    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/user/login")
    public MyResponse login(@RequestParam("qq")String qq, @RequestParam("password")String password) {
        User user1 = userervice.getByQq(Integer.parseInt(qq));
        if(user1==null){
            return new MyResponse(2,"账号不存在");
        }else if(!user1.getPassword().equals(SecureUtil.md5(password))){
            return new MyResponse(2,"密码错误");
        }else{
            return MyResponse.success();
        }
    }

    @ApiOperation(value = "编辑资料")
    @GetMapping(value = "/update")
    public String update(int account, String password) {
        System.out.println("开始更新：");
        userervice.updadteUserPassword(account,password);
        return "更新成功";
    }

    @ApiOperation(value = "查找用户")
    @GetMapping(value = "find")
    public String  findUser() {
        System.out.println(userervice.findAllUser());
        System.out.println("----------完美分割线--------------");
        return "";
    }

    @ApiOperation(value = "注销用户")
    @GetMapping(value = "delete")
    public String  findUser(User User) {
        userervice.deleteUser(User);
        return "删除成功：";
    }
}
